package com.skywolf.chem.usercenterapi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.exception.BizException;
import com.skywolf.chem.usercenterapi.entity.CustomerInfo;
import com.skywolf.chem.usercenterapi.mapper.CustomerInfoMapper;
import com.skywolf.chem.usercenterapi.pojo.LoginParam;
import com.skywolf.chem.usercenterapi.service.ICustomerInfoService;
import com.skywolf.chem.utils.CryptUtil;
import com.skywolf.chem.utils.PhoneUtils;
import com.skywolf.chem.utils.RequestConstans;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.ZoneOffset;

/**
 * <p>
 * app端用户表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-06-09
 */
@Service
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerInfoService {

    @Override
    public JSONObject loginPhone(LoginParam loginParam) {
        QueryWrapper<CustomerInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bind_phone", loginParam.getUsername());
        CustomerInfo user = this.getOne(queryWrapper);
        if (user == null || !user.getPhoneStatus().equals(1)) {
            throw new BizException(ResponseCodeEnum.USER_INVALID_USERNAME_PASSWORD);
        }
        //验证密码是否正确
        Long nowTime = System.currentTimeMillis()/1000;
        Long codeTime = user.getCodeTime().toEpochSecond(ZoneOffset.of("+8"));
        if(nowTime-codeTime>300){
            throw new BizException(ResponseCodeEnum.CODE_EXPIRE);
        }
        if(!loginParam.getMacCode().equals(user.getMscCode())){
            throw new BizException(ResponseCodeEnum.CODE_ERROR);
        }
        String token = user.getId()+"|"+user.getUserName()+"|"+user.getUserPwd()+"|"+System.currentTimeMillis()/1000;
        String userToken = CryptUtil.IMDes3Encode(token, RequestConstans.decode_pwd);
        JSONObject obj = new JSONObject();
        obj.put("userToken",userToken);
        obj.put("userId",user.getId());
        return obj;

    }

    @Override
    public JSONObject loginWechat(String openid, String headImg, String nickName) {
        QueryWrapper<CustomerInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CustomerInfo::getWechatOpenid, openid).eq(CustomerInfo::getDeleted, 0);
        CustomerInfo user = this.getOne(queryWrapper);
        if(user==null){
            user = new CustomerInfo();
            user.setWechatOpenid(openid);
            if(StringUtils.isBlank(headImg)){
                user.setHeadImg("https://chuanti-bucket.oss-cn-chengdu.aliyuncs.com/img/chuanti_head_img.png");
            }else{
                user.setHeadImg(headImg);
            }
            user.setNickName(nickName);
            if(StringUtils.isBlank(nickName)){
                String code = PhoneUtils.getCode();
                user.setNickName("chuanti"+code);
            }
            this.save(user);
        }
        String token = user.getId()+"|"+user.getUserName()+"|"+user.getUserPwd()+"|"+System.currentTimeMillis()/1000;
        String userToken = CryptUtil.IMDes3Encode(token, RequestConstans.decode_pwd);
        JSONObject obj = new JSONObject();
        obj.put("userToken",userToken);
        obj.put("userId",user.getId());
        if(StringUtils.isBlank(user.getBindPhone())){
            obj.put("bindPhone",0);
        }else{
            obj.put("bindPhone",1);
        }
        return obj;
    }

}
